package com.hbyj.struts.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONObject;

import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.YjsjYjsj01;
import com.hbyj.util.Constants;
import com.hbyj.util.PageClass;
import com.opensymphony.xwork2.ActionSupport;

public class EventsHandleAction extends ActionSupport{
	
	private String jsonResult;
	private int total;
	private List rows = new ArrayList();
	
	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}
	
	public void queryEventsNumber() throws UnsupportedEncodingException, IOException{
		//现在的时间
		Date nowTime = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//24小时之前的时间
		Date day1Time = getDateCasePast(nowTime,0);
		String day1TimeStr = sdf.format(day1Time);
		String hql1 = "from YjsjYjsj01 where yjsj01010 >= '"+day1TimeStr+"'";
		int day1EventsNum = DbTools.findByHql(hql1).size();
		//一周之前的时间
		Date day7Time = getDateCasePast(nowTime,1);
		String day7TimeStr = sdf.format(day7Time);
		String hql2 = "from YjsjYjsj01 where yjsj01010 >='"+day7TimeStr+"'";
		int day7EventsNum = DbTools.findByHql(hql2).size();
		//一月之前的时间
		Date month1Time = getDateCasePast(nowTime,2);
		String month1TimeStr = sdf.format(month1Time);
		String hql3 = "from YjsjYjsj01 where yjsj01010 >='"+month1TimeStr+"'";
		int month1EventsNum = DbTools.findByHql(hql3).size();
		//所有未完成的数目
		String hql4 = "from YjsjYjsj01 where yjsj01050 != '02'";
		int noOverEventsNum = DbTools.findByHql(hql4).size();
		JSONObject obj = new JSONObject();
		obj.accumulate("day1", day1EventsNum);
		obj.accumulate("day7", day7EventsNum);
		obj.accumulate("month1", month1EventsNum);
		obj.accumulate("noOver", noOverEventsNum);
		ServletActionContext.getResponse().getOutputStream().write(obj.toString().getBytes("utf-8"));
	}
	
	
	//得到需要的日期，0：一天前，1：一周前，2：一月前
	public static Date getDateCasePast(Date date,int leixing) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		switch(leixing){
		case 0:
			calendar.add(Calendar.DATE, -1);
			break;
		case 1:
			calendar.add(Calendar.DATE, -7);
			break;
		case 2:
			calendar.add(Calendar.MONTH, -1);
			break;
		}
		date = calendar.getTime();
		return date;
	}
	
	
	public String querySjByState(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String state = request.getParameter("state");
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String hql = "from YjsjYjsj01 where yjsj01050 = '"+state+"'";
		String countHql = "select count(*) from YJSJ_YJSJ01 where YJSJ01050 = '"+state+"'";
		int t = DbTools.getDataSQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		rows = (List)DbTools.findPageHQL(first, max,hql);
		total = t;
		return SUCCESS;
	}
	
	
	public String querySjcxByCondition(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String state = request.getParameter("state");
		String sjtybm = request.getParameter("sjtybm");
		String sjdj = request.getParameter("sjdj");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String hql = "from YjsjYjsj01 where  yjsj01050 = '"+state+"' and yjsj01000 like '"+sjtybm+"'";
		String countHql = "select count(*) from YJSJ_YJSJ01 where YJSJ01050 = '"+state+"' and YJSJ01000 like '"+sjtybm+"'";
		if(!"".equals(startTime)){
			hql += " and yjsj01010 >='"+startTime+"' ";
			countHql = " and YJSJ01010 >='"+startTime+"'";
		}
		if(!"".equals(endTime)){
			hql += " and yjsj01010 <='"+endTime+"'";
			countHql += " and YJSJ01010 <='"+endTime+"'";
		}
		if(!"".equals(sjdj)){
			hql += " and yjsj01070 = '"+sjdj+"'";
			countHql += " and YJSJ01070 = '"+sjdj+"'";
		}
		int t = DbTools.getDataSQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		rows = (List)DbTools.findPageHQL(first, max,hql);
		total = t;
		return SUCCESS;
	}
	
}
